<template>
    <div class="talk-box" v-loading="loading">

        <el-table
            :data="tableData"
            border
            style="width: 100%">
            <el-table-column
                label="聊天对象">
                <template slot-scope="scope">
                     <div>趣ID：<span  @click="goUserInfo(scope.row.chatUserId)" class="table-btn">{{scope.row.chatUserId}}</span></div>
                     <div>昵称：{{ scope.row.nickName }}</div>
                </template>
            </el-table-column>
            <el-table-column
                prop="sendMsgCount"
                label="发送消息数">
            </el-table-column>
            <el-table-column
                prop="receiveMsgCount"
                label="接收消息数">
            </el-table-column>
            <el-table-column
                label="创建会话时间">
                <template slot-scope="scope">
                     <div>{{initTime(scope.row.createChatDate)}}</div>
                </template>
            </el-table-column>
            <el-table-column
                label="最后对话时间">
                <template slot-scope="scope">
                    <div>{{initTime(scope.row.lastSeeChatDate)}}</div>
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                width="100">
                <template slot-scope="scope">
                    <div><el-button @click="getChat(scope.row)" type="text" size="small">查看对话记录</el-button></div>
                </template>
            </el-table-column>
        </el-table>

        <div class="tools">
            <el-pagination
                @size-change="sizeChange"
                @current-change="pageChange"
                :current-page="pageNum"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>

        <!-- 客服聊天 -->
        <el-drawer
          size="35%"
          :visible.sync="visible"
          direction="rtl">
          <chat :fromAccount="fromAccount" :chat="chat" @load="getChat"/>
        </el-drawer>
    </div>
</template>

<script>
import { formatDate } from '@/utils/utils'
import chat from '@/views/quDeal/tradOrder/chat'

export default {
    components: {
      chat
    },
    data () {
        return {
            tableData: [],
            loading: false,
            pageNum: 1,
            pageSize: 10,
            total: 0,
            fromAccount: '',
            chat: null,
            visible: false
        }
    },

    props: ['detail'],

    created () {
        if (this.detail && this.detail.userId) {
            this.getDetail()
        }
    },

    methods: {
          getChat (user) {
            this.loading = true
            this.$http.get(`/deal/selectJsonMessageByUserId`, {
              params: { 
                formUserId: user && user.chatUserId,
                toUserId: user && user.userId
              }   
            }).then(res => {
              this.loading = false
              if (res.code === '0') {
                if (!res.data.dealMessageEntryVoList) {
                  this.$message.error('暂无聊天记录')
                  return
                }
                this.chat = res.data
                this.fromAccount = res.data.dealMessageEntryVoList[0].fromAccount
                this.visible = true
              } else {
                this.$message.error(res.desc)
              }
            }).catch(() => {
                this.loading = false
            })
        },
        // 获取详情
        async getDetail () {
            if (this.loading) return
            this.loading = true
            let params = {
                pageNum: this.pageNum,
                pageSize: this.pageSize,
                userId: this.detail.userId
            }
            let res = await this.$http.get('/user/getUserChatRecord', {
                params
            })
            if (res.code === '0') {
                this.tableData = res.data
                this.total = res.count || res.data.length
            } else {
                this.$message({
                    type: 'error',
                    message: res.desc
                })
            }
            this.loading = false
        },

        initTime (time) {
            if (time) {
                return formatDate(new Date(time), 'YYYY-MM-DD HH:MM')
            } else {
                return '-'
            }
        },

        // 查看个人详情
        goUserInfo (id) {
            const { href } = this.$router.resolve({
                path: '/dataReport/userInfo',
                query: {
                    id
                }
            })
            window.open(href, '_blank')
        },

        sizeChange (val) {
            this.pageNum = 1
            this.pageSize = val
            this.getDetail()
        },

        pageChange (val) {
            this.pageNum = val
            this.getDetail()
        },

        statusChange (val) {
            this.pageNum = 1
            this.getDetail()
        }
    }
}
</script>

<style lang="scss" scoped>
    .talk-box {
        .select-box {
            margin-bottom: 10px;
        }

        .point {
            display:inline-block;
            width:10px;
            height:10px;
            border-radius: 50%;
            background-color:#67C23A;
            margin-right:3px;
        }
        .red-point {
            background-color: #F56C6C;
        }
        .table-btn {
            cursor: pointer;
            color: #409EFF;
            font-size: 13px;
            font-weight: 500;
            &:hover {
                text-decoration: underline;
            }
        }

        .tools {
            margin-top: 10px;
            padding-right: 20px;
            display: flex;
            justify-content: flex-end;
        }
    }
</style>
